﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Ppt = Microsoft.Office.Interop.PowerPoint;

namespace ObjectModel.PowerPoint.New
{
    /// <summary>
    /// A PowerPoint TextRange object that can resolve itself from serializable info.
    /// </summary>
    public class ResolvableTextRange : SelectionAwareObject
    {
        Ppt.TextRange mTextRange;

        public SelectionAwareObject Shape { get; set; }

        public int Start { get; set; }

        public int Length { get; set; }

        public ResolvableTextRange() { } // Parameterless constructor required for deserialization.
        
        public ResolvableTextRange(Ppt.TextRange textRange)
        {
            Shape = new ResolvableShape(textRange.GetParent<Ppt.Shape>());
            Start = textRange.Start;
            Length = textRange.Length;
        }

        // Constructor for use with table cell text ranges.
        public ResolvableTextRange(Ppt.TextRange textRange, Ppt.Table table, int row, int col)
        {
            Shape = new ResolvableTableCellShape(table, row, col);
            Start = textRange.Start;
            Length = textRange.Length;
        }

        public override object Resolve()
        {
            Ppt.Shape shape = Shape.Resolve() as Ppt.Shape;
            return shape.TextFrame.TextRange.Characters(Start, Length);
        }

        [XmlIgnore]
        Ppt.TextRange TextRange
        {
            get
            {
                if (mTextRange == null)
                    mTextRange = this.Resolve() as Ppt.TextRange;

                return mTextRange;
            }
        }

        public override bool Selected
        {
            get
            {
                Ppt.Selection sel = Globals.Application.ActiveWindow.Selection;

                if (sel.Type == Ppt.PpSelectionType.ppSelectionText)
                    if (Shape.Selected)
                        return TextRange.Start >= sel.TextRange.Start &&
                               TextRange.Start + TextRange.Length <= sel.TextRange.Start + sel.TextRange.Length;
                
                return false;
            }
        }

        public bool ContainsSelection
        {
            get
            {
                return Shape.Selected && Contains(Globals.Application.ActiveWindow.Selection.TextRange);
            }
        }

        bool Contains(Ppt.TextRange textRangeToCompare)
        {
            // Note: this method assumes that it has already been verified that
            // the two text ranges being compared are from the same text frame.
            int start1 = textRangeToCompare.Start;
            int end1 = textRangeToCompare.Start + textRangeToCompare.Length;
            int start2 = TextRange.Start;
            int end2 = TextRange.Start + TextRange.Length;

            return start1 >= start2 && end1 <= end2;
        }
    }

    ///// <summary>
    ///// A PowerPoint TextRange object that can resolve itself from serializable info.
    ///// </summary>
    //public class ResolvableTextRange : SelectionAwareObject
    //{
    //    public SelectionAwareObject Shape { get; set; }

    //    public int Start { get; set; }

    //    public int Length { get; set; }

    //    public ResolvableTextRange() { } // Parameterless constructor required for deserialization.

    //    public ResolvableTextRange(Ppt.TextRange textRange)
    //    {
    //        Shape = new ResolvableShape(textRange.GetParent<Ppt.Shape>()); ;
    //        Start = textRange.Start;
    //        Length = textRange.Length;
    //    }

    //    // Constructor for use with table cell text ranges.
    //    public ResolvableTextRange(Ppt.TextRange textRange, Ppt.Table table, int row, int col)
    //    {
    //        Shape = new ResolvableTableCellShape(table, row, col);
    //        Start = textRange.Start;
    //        Length = textRange.Length;
    //    }

    //    public override object Resolve()
    //    {
    //        Ppt.Shape shape = Shape.Resolve() as Ppt.Shape;
    //        return shape.TextFrame.TextRange.Characters(Start, Length);
    //    }

    //    public override bool Selected
    //    {
    //        get
    //        {
    //            Ppt.Selection sel = Globals.Application.ActiveWindow.Selection;

    //            if (sel.Type == Ppt.PpSelectionType.ppSelectionText)
    //                if (Shape.Selected)
    //                    return Start >= sel.TextRange.Start &&
    //                           Start + Length <= sel.TextRange.Start + sel.TextRange.Length;

    //            return false;
    //        }
    //    }

    //    public bool ContainsSelection
    //    {
    //        get
    //        {
    //            return Shape.Selected && Contains(Globals.Application.ActiveWindow.Selection.TextRange);
    //        }
    //    }

    //    bool Contains(Ppt.TextRange textRangeToCompare)
    //    {
    //        // Note: this method assumes that it has already been verified that
    //        // the two text ranges being compared are from the same text frame.
    //        int start1 = textRangeToCompare.Start;
    //        int end1 = textRangeToCompare.Start + textRangeToCompare.Length;
    //        int start2 = Start;
    //        int end2 = Start + Length;

    //        return start1 >= start2 && end1 <= end2;
    //    }
    //}
}
